package impl.repository;

import interfaces.repository.CustomerRepository;
import interfaces.repository.ProductApparelRepository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import dao.AccessDatabase;
import domain.Customer;

public class StubCustomerRepositoryImpl implements CustomerRepository {

	@Override
	public Customer findByID(int idBilling) {

		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		StubShippingRepositoryImpl ssri = new StubShippingRepositoryImpl();
		StubBillingRepositoryImpl sbri = new StubBillingRepositoryImpl();
		StubCartItemRepositoryImpl scir = new StubCartItemRepositoryImpl();

		Customer customer = null;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM `customer` where `idBilling`='"
							+ idBilling + "';");

			customer = new Customer(

			rs.getInt("idcustomer"), rs.getString("logon"),
					rs.getString("name"), rs.getString("email"),
					rs.getString("password"), sari.findById(rs
							.getInt("idAdress")), rs.getString("category"),
					ssri.findByID(rs.getInt("idcustomer")), sbri.findById(rs
							.getInt("idCustomer")), scir.findAllbyIDCustomer(rs
							.getInt("idcustomer")));

			con.close();
			return customer;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}

	}

	@Override
	public List<Customer> findByName(String name) {

		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		StubShippingRepositoryImpl ssri = new StubShippingRepositoryImpl();
		StubBillingRepositoryImpl sbri = new StubBillingRepositoryImpl();
		StubCartItemRepositoryImpl scir = new StubCartItemRepositoryImpl();

		List<Customer> customers = new ArrayList<Customer>();
		Customer customer = null;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM `customer` where `name`LIKE '%"
							+ name + "%'");

			while (rs.next()) {

				customer = new Customer(

				rs.getInt("idcustomer"), rs.getString("logon"),
						rs.getString("name"), rs.getString("email"),
						rs.getString("password"), sari.findById(rs
								.getInt("idAdress")), rs.getString("category"),
						ssri.findByID(rs.getInt("idcustomer")),
						sbri.findById(rs.getInt("idcustomer")),
						scir.findAllbyIDCustomer(rs.getInt("idcustomer")));

				customers.add(customer);

			}
			con.close();
			return customers;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}

	}

	@Override
	public boolean add(Customer customer) {

		if (this.HasLogon(customer.getLogon()) == null) {

			try {

				AccessDatabase a = new AccessDatabase();
				Connection con = a.conectar();
				Statement st = con.createStatement();
				st.execute("INSERT INTO `customer` (`logon`,`name`,`email`,`password`,`idAdress`,`category`,`idShipping`,`idBilling`) VALUES ('"
						+ customer.getLogon()
						+ "','"
						+ customer.getName()
						+ "','"
						+ customer.getEmail()
						+ "','"
						+ customer.getPassword()
						+ "','"
						+ customer.getAddress()
						+ "','"
						+ customer.getCategory()
						+ "','"
						+ customer.getShipping()
						+ "','"
						+ customer.getBilling() + "');");
				con.close();

				return true;
			} catch (Exception t) {

				return false;

			}
		} else {
			System.out.println("Usuario ja cadastrado!!!");
			return false;
		}
	}

	@Override
	public boolean edit(Customer before, Customer after) {

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("UPDATE `customer` SET `name`='" + after.getName()
					+ "', `email`='" + after.getEmail() + "', `password`='"
					+ after.getPassword() + "', `category`='"
					+ after.getCategory() + "' WHERE `idAddress`='"
					+ before.getId() + "'");
			con.close();
			return true;
		} catch (Exception t) {
			JOptionPane.showMessageDialog(null, t.getMessage());
			return false;
		}

	}

	@Override
	public boolean delete(Customer customer) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `customer` WHERE `idcustomer`='"
					+ customer.getId() + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public List<Customer> findAllCustomer() {

		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		StubShippingRepositoryImpl ssri = new StubShippingRepositoryImpl();
		StubBillingRepositoryImpl sbri = new StubBillingRepositoryImpl();
		StubCartItemRepositoryImpl scir = new StubCartItemRepositoryImpl();

		List<Customer> customers = new ArrayList<Customer>();
		Customer customer = null;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM `customer`");

			while (rs.next()) {

				customer = new Customer(

				rs.getInt("idcustomer"), rs.getString("logon"),
						rs.getString("name"), rs.getString("email"),
						rs.getString("password"), sari.findById(rs
								.getInt("idAdress")), rs.getString("category"),
						ssri.findByID(rs.getInt("idcustomer")),
						sbri.findById(rs.getInt("idcustomer")),
						scir.findAllbyIDCustomer(rs.getInt("idcustomer")));

				customers.add(customer);

			}
			con.close();
			return customers;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}
	}

	@Override
	public Customer findByLogin(String login) {

		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		StubShippingRepositoryImpl ssri = new StubShippingRepositoryImpl();
		StubBillingRepositoryImpl sbri = new StubBillingRepositoryImpl();
		StubCartItemRepositoryImpl scir = new StubCartItemRepositoryImpl();

		Customer customer;
		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM `customer` where `logon`='"
							+ login + "';");

			customer = new Customer(

			rs.getInt("idcustomer"), rs.getString("logon"),
					rs.getString("name"), rs.getString("email"),
					rs.getString("password"), sari.findById(rs
							.getInt("idAdress")), rs.getString("category"),
					ssri.findByID(rs.getInt("idcustomer")), sbri.findById(rs
							.getInt("idcustomer")), scir.findAllbyIDCustomer(rs
							.getInt("idcustomer")));

			con.close();
			return customer;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}

	}

	@Override
	public Customer HasLogon(String logon) {
		
		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		StubShippingRepositoryImpl ssri = new StubShippingRepositoryImpl();
		StubBillingRepositoryImpl sbri = new StubBillingRepositoryImpl();
		StubCartItemRepositoryImpl scir = new StubCartItemRepositoryImpl();

		Customer customer = null;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM `customer` where `logon`='"
							+ logon + "';");

			customer = new Customer(

			rs.getInt("idcustomer"), rs.getString("logon"),
					rs.getString("name"), rs.getString("email"),
					rs.getString("password"), sari.findById(rs
							.getInt("idAdress")), rs.getString("category"),
					ssri.findByID(rs.getInt("idcustomer")), sbri.findById(rs
							.getInt("idCustomer")), scir.findAllbyIDCustomer(rs
							.getInt("idcustomer")));

			con.close();
			return customer;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}
	}

	@Override
	public boolean VerifyCustomer(String logon, String password) {
		Customer customer = this.HasLogon(logon);
		
		if(customer.getPassword().equals(password)){
			
			return true;
	
		}else{
		
		return false;
		}
	}

}
